package utils

import (
	"github.com/gin-gonic/gin"
	"strings"
	"gitee.com/ifinder/gocomm/protocol/resp"
	"strconv"
)

func DoResponse(code int,c *gin.Context,data ...interface{})  {

	contentType:=strings.ToLower(c.GetHeader("Content-Type"))

	var d interface{}
	if len(data)!=0{
		d=data[0]
	}else{
		d=resp.NewSimpleResponse()
	}

	if strings.Index(contentType,"application/json")!=-1{
		c.JSON(code, d)
	}else if strings.Index(contentType,"application/javascript")!=-1{
		c.JSONP(code,d)
	}else if strings.Index(contentType,"application/xml")!=-1{
		c.XML(code, d)
	}else{
		c.JSON(code, d)
	}
}

func GetPageNo(c *gin.Context)  int{
	pageNo:=GetInt(c,"pageNo",1)

	if pageNo==0{
		pageNo=1
	}
	return pageNo
}
func GetPageSize(c *gin.Context) int {
	pageSize:=GetInt(c,"pageSize",1)

	if pageSize==0{
		pageSize=10
	}
	return pageSize
}

func GetInt(c *gin.Context,name string,defaultValue int) int {
	v:=c.GetString(name)
 	if  v==""{
 		return defaultValue
	}
	i,_:=strconv.Atoi(v)

	return i
}
